package com.hjsmallfly.syllabus.repository.mock_lesson

import com.hjsmallfly.syllabus.bean.SemesterData
import com.hjsmallfly.syllabus.ext.to_ui
import com.hjsmallfly.syllabus.room.record.MockLesson
import rx.Observable

class MockLessonDataRepository(
    private val localDataSource: MockLessonDataSource,
    private val remoteDataSource: MockLessonDataSource
) : MockLessonDataSource {

    override fun queryNextSemester(): Observable<SemesterData> {
        return remoteDataSource.queryNextSemester()
            .onErrorResumeNext(localDataSource.queryNextSemester())
            .to_ui()
            .doOnNext {
                saveNextSemester(nextSemester = it)
            }
    }

    override fun saveNextSemester(nextSemester: SemesterData) = localDataSource.saveNextSemester(nextSemester)

    override fun queryMockLesson(uid: Long, semester: SemesterData) = localDataSource.queryMockLesson(uid, semester)

    override fun saveMockLesson(uid: Long, mockLesson: MockLesson) = localDataSource.saveMockLesson(uid, mockLesson)
}